<template>
    <!--我的收货地址-->
    <div class="address">
      <div class="addresslist">
        <div class="address_item p_l30 p_r30 bg_colorff m_t20" v-for="(add,index) in addresslist" :key="index" @click="chooseaddidFn(add.id, add.detail, add.address,add.real_name,add.mobile)">
          <div class="border_b color_33 p_t30 p_b30">
            <div class="userinfo font_size32"><span>{{add.real_name}}</span><span class="m_l20">{{add.mobile}}</span></div>
            <div class="addressinfo font_size28 m_t20">{{add.detail}}{{add.address}}</div>
          </div>
          <div class="setaddress flex">
            <div class=" font_size28" @click="setdefaultaddFn(add.id)">
              <!--<img class="addressch" :src="add.ismoren==0?'../assets/img/circle.png': '../assets/img/circle_checked.png'"/>-->
              <img v-if="add.is_default==0" class="addressch" src="../../assets/img/icon/circle.png"/>
              <img v-if="add.is_default==1" class="addressch" src="../../assets/img/icon/circle_checked.png"/>
              <span :class="add.is_default==0?'color_33':'color_0d'">默认地址</span>
            </div>
            <div class="flex-1 t_right font_size24"><span class="address_span" @click="updateFn(add.id)">修改</span><span @click="showdellayer(add.id, index)" class="address_span">删除</span></div>
          </div>

        </div>
      </div>

      <div class="addbtn" @click="goaddFn">新增地址</div>

      <div class="layer" v-if="isdel">
        <div class="layer_box color_33">
          <div class="tiptitle font_size32 border_b t_center">是否删除该地址</div>
          <div class="flex t_center line80 font_size24 ">
            <div class="flex-1" @click="deladd(1)">确认</div>
            <div class="flex-1 border_l" @click="deladd(2)">取消</div>
          </div>
        </div>
      </div>

    </div>
</template>

<script>
    export default {
      name: "center_address",
      data(){
        return{
          addresslist: [],
          isdel: false,
          delid: '',
          delindex:'',
          pagetype: ''
        }
      },
      created(){
        this.pagetype = this.$route.query.type
        this.getaddListFn()
      },
      methods:{
        showdellayer(id, index){
          this.isdel = !this.isdel
          this.delid = id
          this.delindex = index
        },
        deladd(type){
          var self = this
          self.isdel = !self.isdel
          if(type == 1){
            this.$util.get('address/deleteUserAddress',{
              uid: this.$store.state.userinfo.uid,
              id: self.delid
            }).then(data =>{
              console.log(self.delindex)
              if(data.status ==  1){
                self.addresslist.splice(self.delindex, 1)
              }
            })
          }

        },
        updateFn(id){
          this.$router.push({path: 'addaddress',query: {id : id}})
        },
        //获取列表
        getaddListFn(){
          var self = this
          this.$util.get('address/getUserAddressList',{
            uid: this.$store.state.userinfo.uid
          }).then(data =>{
            if(data.status ==  1){
              self.addresslist = data.data
            }
          })
        },
        // 设置默认值
        setdefaultaddFn(id){
          var self = this
          this.$util.get('address/setDefaultAddress',{
            uid: this.$store.state.userinfo.uid,
            id: id
          }).then(data => {
            self.$toast('设置成功')
            self.getaddListFn()
          })
        },
        goaddFn(){
          this.$router.push('/addaddress')
        },
        chooseaddidFn(id,citys,infos,username,userphone){
          if(this.pagetype){
            var c_info = citys+infos
            this.$store.commit('setaddressid',id)
            this.$store.commit('setaddresscityinfo',c_info)
            this.$store.commit('setaddressusername',username)
            this.$store.commit('setaddressuserphone',userphone)
            this.$router.back()
          }
        }
      }
    }
</script>

<style lang="less" scoped>
  @import "../../assets/css/public.less";
  @import "../../assets/css/center/address";
</style>
